package com.jiangnan.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * 日期转化类
 *
 * @author chenliang
 * @email wschenliang@aliyun.com
 */
public class DateUtil {

    public static final String NORMAL_DATE_TIME_FORMAT_STR = "yyyy-MM-dd HH:mm:ss";

    public static Date valueOf(Object value){
        if(value == null || "null".equals(value)){
            return null;
        }
        if(value instanceof Date){
            return (Date)value;
        }else if(value instanceof Long){
            long val = (Long)value;
            return val == 0L ? null : new Date(val);
        }else{
            String str = value.toString();
            if(StringUtils.isBlank(str)) {
                return null;
            }
            try {
                long val = Long.parseLong(str);
                return val == 0L ? null : new Date(val);
            } catch (RuntimeException e) {
                try {
                    DateFormat df = new SimpleDateFormat(NORMAL_DATE_TIME_FORMAT_STR);
                    return df.parse(str);
                } catch (ParseException e1) {
                    throw new RuntimeException(e1);
                }
            }
        }
    }
}
